Handlebars.registerHelper('grid', function(context, cols, options) {
	var ret = '<table class="members-grid members-grid-' + cols + '"><tbody>';

	for(var i = 0; i < context.length; i += cols) {
		ret += '<tr>';
		for(var j = 0; j < cols; j++) {			
			var member = context[i + j];
			if (i + j < context.length) {
				ret += '<td class="members-grid-item" data-card-id="' + member.card.id + '">' + options.fn(member) + '</td>';
			} else {
				ret += '<td>&nbsp;</td>';
			}		
		}
		ret += '</tr>';
	}

	return ret + '</tbody></table>';
});

Handlebars.registerHelper('loc', function(context, options) {
	var ret = conquest.static.messages[context];
	if (_.isUndefined(ret)) {
		ret = context;
	}
	return ret;
});

Handlebars.registerHelper('for', function(from, to, incr, options) {
	var ret = '';
	for(var i = from; i <= to; i += incr) {
		ret += options.fn(i);
	}
	return ret;
});

Handlebars.registerHelper('factionImagePath', function(context, options) {
	return conquest.util.toFactionImageMd(context);
});

Handlebars.registerHelper('factionImagePathLg', function(context, options) {
	return conquest.util.toFactionImageLg(context);
});

Handlebars.registerHelper('cardImagePath', function(context, options) {
	// cardId or cardImageBase
	if (_.isNumber(context)) {
		return conquest.util.toCardImage(conquest.dict.findCard(context).imageBase);
	} else {
		return conquest.util.toCardImage(context);	
	}	
});

Handlebars.registerHelper('findCard', function(context, options) {
	return conquest.dict.findCard(context);
});

Handlebars.registerHelper('findSet', function(context, options) {
	return conquest.dict.findSet(context);
});

Handlebars.registerHelper('rootUrl', function(options) {
	return conquest.static.root;
});

Handlebars.registerHelper('cardUrl', function(context, options) {
	return conquest.util.toCardUrl(context);
});

Handlebars.registerHelper('cardRelativeUrl', function(context, options) {
	return conquest.util.toCardRelativeUrl(context);
});


Handlebars.registerHelper('href', function(context, options) {
	var ret = conquest.root + '#';
	return conquest.root + '#/' + context;
});

Handlebars.registerHelper('concat', function(c0, c1, c2, c3, c4, options) {
	var ret = '';
	_.each([c0, c1, c2, c3, c4], function(item) {
		if (!_.isUndefined(item)) {
			ret += item;
		}
	});
	return ret;
});

Handlebars.registerHelper('ifSmart', function(context, options) {
	if (_.isUndefined(context) || context === null || context === false) {
		return options.inverse(this);
	} else {
		return options.fn(this);
	}
});